home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.lib;
- import sub_arctic.input.*;
- import sub_arctic.output.*;
-
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Color;
- import java.awt.Dimension;
-
- /**
- * This class implements a menu_item with a string of text on it.
- *
- * @see menu
- * @author Ian Smith
- */
- public class text_menu_item extends menu_item {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The text string on this object.
- */
- protected String _text;
-
- /**
- * Return the text on this object.
- * @return String the string of text on this object.
- */
- public String text() { return _text;};
-
- /**
- * Set the string on this object. This function does NOT
- * imply a re-computation of the width of this object.
- *
- * @param String s the new text string for this object.
- */
- public void set_text(String s) {
- _text=s;
- style_changed();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This objects font. It will be null if they want the system
- * default font.
- */
- protected Font _font=null;
-
- /**
- * Return this object's font. This will return the system's default
- * font if you don't set the font explicitly.
- * @return Font the font in use for this object.
- */
- public Font font() {
- if (_font==null) {
- return style_manager.default_font();
- } else {
- return _font;
- }
- }
-
- /**
- * Set the font used for this object. Set this to null if you want
- * the system default font.
- * @param Font f the font for this object.
- */
- public void set_font(Font f) {
- _font=f;
- style_changed();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The amount of border to use in x.
- */
- protected int _x_border = 2;
-
- /**
- * Return the size of the x border.
- * @return int the number of pixels of horizontal spacing (appears on both
- * right and left).
- */
- public int x_border() { return _x_border;}
-
- /**
- * Change the amount of horizontal spacing
- * @param int v the new amount of x spacing.
- */
- public void set_x_border(int v) {
- _x_border=v;
- style_changed();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The amount of border to use in y.
- */
- protected int _y_border=1;
-
- /**
- * Return the size of the y border.
- * @return int the number of pixels of vertical spacing (appears on both
- * top and bottom).
- */
- public int y_border() { return _y_border;}
-
- /**
- * Change the amount of vertical spacing.
- * @param int v the new amount of y spacing.
- */
- public void set_y_border(int v) {
- _y_border=v;
- style_changed();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Store the two looks for this object.
- */
- protected loaded_image[] _look;
-
- /*
- * Compute the new images for this object.
- */
- protected void style_changed() {
- style cs=style_manager.current_style();
-
- _look=cs.menu_item_image(text(),font(),x_border(),y_border(),
- w(),h(),false);
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Draw yourself... depends the value of the highlight func.
- * defined in menu_item.
- * @param drawable d the surface to draw yourself on.
- */
- protected void draw_self_local(drawable d) {
- FontMetrics metrics=manager.get_metrics(font());
- Color fg,bg;
-
- /* if highlighted draw down, otherwise draw up */
- if (highlighted()) {
- d.drawImage(_look[1],0,0);
- } else {
- d.drawImage(_look[0],0,0);
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return that our width and height are intrinsically computed.
- * @return int the constant value of W | H
- */
- public int intrinsic_constraints() {
- return (W | H);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Construct a text menu item given a string, a font, and a width.
- * @param String s the string to be displayed on the item.
- * @param Font f the font to use for this item.
- * @param int width the width of this object.
- */
- public text_menu_item(String s, Font f, int width)
- {
- super();
-
- Dimension d;
- style cs=style_manager.current_style();
-
- _font=f;
- _text=s;
- d=cs.menu_item_natural_size(s,font(),2,1);
- set_intrinsic_w(width);
- set_intrinsic_h(d.height);
- style_changed();
- }
-
- //had:
- //* @exception general PROPAGATED.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Simple default version of a constructor which doesn't require
- * a font.
- * @param String s the string to be displayed on the item.
- * @param int width the width of this object.
- */
- public text_menu_item(String s,int width)
- {
- super();
-
- style cs=style_manager.current_style();
- Dimension d=cs.menu_item_natural_size(s,font(),2,1);
-
- _text=s;
- set_intrinsic_w(width);
- set_intrinsic_h(d.height);
- style_changed();
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return a debugging string.
- * @return String a string representing this object and its position.
- */
- public String toString() {
- return "text_menu_item: [" + x() + "," + y() + "]";
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This function is a convenience for figuring out which of a set
- * of strings will have the widest menu and item and what the
- * widest value is. It assumes the default spacing of 2 in x and
- * 1 in y.
- *
- * @param String[] strings the array of strings to consider.
- * @param Font f the font you'll be using to render these
- * strings (use null for a default font).
- * @return int the width of the widest of these strings
- */
- public static int max_item_width(String[] strings, Font f) {
- Font font;
- int max=-1,i;
- Dimension d;
- style cs=style_manager.current_style();
-
- /* get the font right */
- if (f==null) {
- font=style_manager.default_font();
- } else {
- font=f;
- }
-
- /* loop over all the strings */
- for (i=0; i<strings.length; ++i) {
- d=cs.menu_item_natural_size(strings[i],font,2,1);
- /* if its bigger, make it the max */
- if (d.width>max) max=d.width;
- }
-
- /* tell the user how big it was */
- return max;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-